import 'package:isar/isar.dart';
import 'package:path_provider/path_provider.dart';
import 'medicine_model.dart';

class IsarService {
  static final IsarService _instance = IsarService._internal();
  late Future<Isar> _isarInstance;

  factory IsarService() {
    return _instance;
  }

  IsarService._internal() {
    _isarInstance = _openIsar();
  }

  Future<Isar> _openIsar() async {
    final dir = await getApplicationDocumentsDirectory();
    return Isar.open(
      [MedicineSchema],
      directory: dir.path,
      inspector: true,
    );
  }

  Future<Isar> get isarInstance => _isarInstance;

  Future<void> closeIsar() async {
    final isar = await _isarInstance;
    await isar.close();
  }
}